﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using FAQFrameworks;
using PhysicalDataProvider;
using FooFun.DataElements.Dtos;
using System.Data.Common;
using DTOFrameworks;

namespace FAQFrameworks
{
    public class EventManager
    {
        EventService eventService;

        DEDataProvider dataProvider;

        public EventManager()
        {
            dataProvider = new DEDataProvider();

            eventService = new EventService();
            eventService.DataProvider = dataProvider;
        }

        public IDataElement GetElementFromDB(IDataElement de)
        {
            dataProvider.Begin();

            IDataElement objDE = eventService.GetElement(de);

            dataProvider.End();

            return objDE;
        }

        public IDataElement GetElement(IDataElement de)
        {
            if (CacheManager.Init[GetKey(de)] == null)
            {
                IDataElement eventDE = GetElementFromDB(de);

                CacheManager.Init[GetKey(de)] = eventDE;

                return eventDE;
            }

            return CacheManager.Init[GetKey(de)] as IDataElement;
        }

        public IDataElement GetElement(int eventId)
        {
            return GetElement(new IntDataElement(eventId));
        }

        string GetKey(object eventId)
        {
            return "eventId_" + eventId;
        }

        //public Event GetEvent(int eventId)
        //{
        //    DataTable table = DataService.FillTable(
        //        "SELECT * FROM t_Events WHERE EventId =" + eventId);

        //    if (table.Rows.Count == 0)
        //    {
        //        return null;
        //    }

        //    DataRow row = table.Rows[0];

        //    Event e = new Event();

        //    e.Action = (int)row["Action"];
        //    e.Description = (string)row["Description"];
        //    e.EventId = (int)row["EventId"];
        //    e.IP = (string)row["IP"];
        //    e.Time = (DateTime)row["Time"];
        //    e.UserId = (int)row["UserId"];

        //    return e;
        //}

        //public IDataElement GetElement(object eventId)
        //{
        //    Event ev = null;
        //    try
        //    {
        //        ev = GetEvent((int)eventId);
        //    }
        //    catch
        //    {
        //        // todo: 引入日志系统进行记录            
        //    }
        //    if (ev != null)
        //    {
        //        return new ObjectDataElement(ev);
        //    }
        //    else
        //    {
        //        return new EmptyDataElement();
        //    }
        //}

        //public IDataElement GetElement(IDataElement de)
        //{
        //    Event ev = null;
        //    try
        //    {
        //        ev = GetEvent(Convert.ToInt32(de.Value));
        //    }
        //    catch
        //    {
        //        // todo: 引入异常的日志系统进行记录
        //    }
        //    if (ev != null)
        //    {
        //        return new ObjectDataElement(ev);
        //    }
        //    else
        //    {
        //        return new EmptyDataElement();
        //    }
        //}

        public IDataElement ListLastReplyEventAboutUser
            (Guid userId, DateTime time)
        {
            using (DbConnection conn = SqlHelper.Connection)
            {
                DbCommand cmd = conn.CreateCommand();

                cmd.CommandText = @"SELECT t_Events.*,
t_TopicIndex.TopicId
FROM t_Events,t_TopicIndex
WHERE t_Events.CreateTime > ?
AND t_Events.Action = ?
AND t_TopicIndex.UserId=?
AND t_TopicIndex.TopicId=t_Events.TargetId";

                conn.Open();

                DbParameter para1 = cmd.CreateParameter();
                para1.Value = time.ToString();

                DbParameter para2 = cmd.CreateParameter();
                para2.Value = (int)FAQActionEnum.AddReply;

                DbParameter para3 = cmd.CreateParameter();                
                para3.Value = DataProviderUtil.GetGuidBase64(userId);

                cmd.Parameters.AddRange(
                    new DbParameter[] { para1, para2, para3 }
                    );

                DbDataReader dr = cmd.ExecuteReader();

                IDataElement de = DataReaderEmit.GetDataElement(dr, "ReplyTopic");

                dr.Close();

                conn.Close();

                return new TopicIndexDataElement(de);
            }
        }
    }
}
